package com.ruoyi.clf.controller;

import com.ruoyi.clf.service.impl.OrderAggregationService;
import com.ruoyi.clf.service.impl.OrderAggregationService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jingying")
public class JingYingTongJiController {

    @Autowired
    private OrderAggregationService orderAggregationService;  // 收入、订单量
    @Autowired
    private OrderAggregationService2 orderAggregationService2;  // 收入分布、订单分布

    @GetMapping("/tongji")
    public Object getOrderAggregation(
            @RequestParam("merchantId") Long merchantId,
            @RequestParam("date") String date
    ) throws IOException {
        // 1. 从 OrderAggregationService 获取收入和订单量数据
        Map<String, Object> revenueData = getMerchantDateData(
                orderAggregationService.getOrderAggregation(),
                merchantId,
                date
        );

        // 2. 从 OrderAggregationService2 获取收入分布和订单分布数据
        Map<String, Object> distributionData = getMerchantDateData(
                orderAggregationService2.getOrderAggregation(),
                merchantId,
                date
        );

        // 3. 合并两个服务的结果
        Map<String, Object> result = new HashMap<>();
        result.put("merchantId", merchantId);
        result.put("date", date);
        result.put("revenueData", revenueData);      // 收入、订单量
        result.put("distributionData", distributionData);  // 收入分布、订单分布

        return result;
    }

    // 提取公共的过滤逻辑为私有方法
    private Map<String, Object> getMerchantDateData(
            List<Map<String, Object>> allData,
            Long merchantId,
            String date
    ) {
        // 遍历查找指定商户ID的商户
        Map<String, Object> targetMerchant = null;
        for (Map<String, Object> merchant : allData) {
            if (merchant.get("merchant_id").equals(merchantId)) {
                targetMerchant = merchant;
                break;
            }
        }

        if (targetMerchant == null) {
            // 使用 HashMap 替代 Map.of (兼容 Java 8)
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("error", "商户ID [" + merchantId + "] 不存在");
            return errorMap;
        }

        // 从商户中提取日期列表
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> dates = (List<Map<String, Object>>) targetMerchant.get("dates");

        // 遍历查找指定日期的记录
        Map<String, Object> targetDate = null;
        for (Map<String, Object> dateData : dates) {
            if (dateData.get("date").equals(date)) {
                targetDate = dateData;
                break;
            }
        }

        if (targetDate == null) {
            // 使用 HashMap 替代 Map.of (兼容 Java 8)
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("error", "商户ID [" + merchantId + "] 在日期 [" + date + "] 无数据");
            return errorMap;
        }

        return targetDate;
    }
}